package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.Attachment;
import com.hcrazy.manchester.app.pojo.PageModel;

public class AttachmentDAO extends BaseDAO<Attachment>{

	public void addAttachment(Attachment attachment) throws Exception{
		StringBuilder sb = new StringBuilder();
		sb.append("insert into him_forum_attachment(");
		sb.append("fileName,fileType,size,uid,username,attachmentTime,file,fileIcon,forumId");
		sb.append(") values (");
		sb.append(":fileName,:fileType,:size,:uid,:username,:attachmentTime,:file,:fileIcon,:forumId");
		sb.append(")");
		
		update(sb.toString(), attachment);
	}
	
	public List<Attachment> listAllInfos() throws Exception{
		String sql = "select * from him_forum_attachment";
		return getList(sql, Attachment.class);
	}
	
	public PageModel getPagedInfos(PageModel model,String fileName,String username,String startTime,String endTime,long forumId) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from (select a.*,b.`name` as groupName from him_forum_attachment a,him_forum_forum b where a.forumId = b.forumId) as c");
		StringBuilder countSQL = new StringBuilder("select count(*) from (select a.*,b.`name` as groupName from him_forum_attachment a,him_forum_forum b where a.forumId = b.forumId) as c");
		StringBuilder whereSQL = new StringBuilder(" where fileName like ?");
		List<Object> paramList = new ArrayList<Object>();
		paramList.add("%"+fileName+"%");
		
		if(null != username && !"".equals(username)){
			whereSQL.append(" and username = ?");
			paramList.add(username);
		}
		
		if(null != startTime && null != endTime && (!"".equals(startTime) && !"".equals(endTime))){
			whereSQL.append(" and attachmentTime > ? and attachmentTime < ?");
			paramList.add(startTime);
			paramList.add(endTime);
		}
		
		if(forumId > 0){
			whereSQL.append(" and forumId = ?");
			paramList.add(forumId);
		}
		System.out.println(querySQL.toString() + whereSQL.toString());
		System.out.println(paramList);
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Attachment.class);
	}
	
	/**
	 * 客户端请求学习小组共享资料列表
	 * @param model
	 * @param forumId
	 * @return
	 * @throws Exception
	 */
	public PageModel getAttachments(PageModel model,long forumId) throws Exception{
		StringBuilder querySQL = new StringBuilder("select * from (select a.*,b.`name` as groupName from him_forum_attachment a,him_forum_forum b where a.forumId = b.forumId) as c");
		StringBuilder countSQL = new StringBuilder("select count(*) from (select a.*,b.`name` as groupName from him_forum_attachment a,him_forum_forum b where a.forumId = b.forumId) as c");
		StringBuilder whereSQL = new StringBuilder(" where 1=1 ");
		List<Object> paramList = new ArrayList<Object>();
		
		if(forumId > 0){
			whereSQL.append(" and forumId = ?");
			paramList.add(forumId);
		}
		
		whereSQL.append(" order by attachmentTime desc");
		return this.getPageModel(model, querySQL, countSQL, whereSQL, paramList, Attachment.class);
	}
	
	public Attachment getById(long attachmentId) throws Exception{
		String sql = "select * from him_forum_attachment where attachmentId = ?";
		return this.getJavaBean(sql, Attachment.class, attachmentId);
	}
	
	public void delAttachment(long attachmentId) throws Exception{
		String sql = "delete from him_forum_attachment where attachmentId = ?";
		this.commonUpdate(sql, attachmentId);
	}
	
}
